let db              = require('../db/dbmanager');
const constants     = require('../data/constants');
const log4js        = require('../data/log');
/**
 * 设备表操作
 */
let Camera = {
    /**
     * 插入数据
     * @param {*} data ["testcamera",false]
     */
    insert:function(data,callback){
        let param ={
            $deviceid:data.deviceid,
            $password:data.password,
            $online  :data.online
        };
        const sql = "insert into CAMERA(deviceid,password,online) values($deviceid,$password,$online)";
        db.sqliteDB.run(sql,param);
        db.sqliteDB.lastRow("CAMERA",(d)=>{
            callback(d);
        });
    },
    /**
     * 更新内容,newdata的属性有; online,deviceid,sip_command_host,sip_command_port
     * @param {*} newdata 
     */
    update:function(newdata,callback){
        let sqldata={
            $deviceid: newdata.deviceid
        };
        
        let newsql='';
        if(newdata.online){
            sqldata.$online = newdata.online;
            if(newsql)newsql+=',';
            newsql += "online=$online";
        }
        if(newdata.sip_command_host){
            sqldata.$sip_command_host = newdata.sip_command_host;
            if(newsql)newsql+=',';
            newsql += "sip_command_host=$sip_command_host";
        }
        if(newdata.sip_command_port){
            sqldata.$sip_command_port = newdata.sip_command_port;
            if(newsql)newsql+=',';
            newsql += "sip_command_port=$sip_command_port";
        }
        if(newdata.sumnum){
            sqldata.$sumnum = newdata.sumnum;
            if(newsql)newsql+=',';
            newsql += "sumnum=$sumnum";
        }
        if(newdata.version){
            sqldata.$version = newdata.version;
            if(newsql)newsql+=',';
            newsql += "version=$version";
        }
        
        const sql = "update CAMERA set " + newsql + " where deviceid=$deviceid";
        db.sqliteDB.run(sql,sqldata);
        if(callback){
            callback(newdata);
        }
    },
    /**
     * 
     * @param {string} deviceid 通道号
     * @param {*} callback 
     */
    getCameraByName:function(deviceid,callback){
        const sql = "SELECT * FROM CAMERA WHERE deviceid=$deviceid";
        db.sqliteDB.all(sql,{$deviceid:deviceid},function(err,rows){
            if(!err){
                callback(rows);
            }else{
                callback();
            }
        });
    },
    /**
     * 设备设置为在线状态
     * @param {*} deviceid 
     * @param {*} callback 
     */
    updateOnline:function(deviceid,callback){
        let me = this;
        me.getCameraByName(deviceid,(ret)=>{
            if(ret.length){
                me.update(constants.registry[deviceid],(d)=>{
                    callback(deviceid);
                });
            }else{
                me.insert(constants.registry[deviceid],(d)=>{
                    callback(deviceid);
                });
            }
        });
    },
    /**
     * 查询列表
     * @param {*} callback 
     */
    list:function(){
        let p = new Promise(function(resolve,reject){
            const sql="SELECT * FROM CAMERA";
            db.sqliteDB.queryData(sql,function(rows){
                resolve(rows);
            });
        });
        return p;
    },
    // 重置状态
    reset:function(){
        const sql = "UPDATE CAMERA SET online=0";
        db.sqliteDB.run(sql);
    }
};

module.exports = Camera;